<script setup>
import { ref, watch } from 'vue'
import { useRouter } from 'vue-router'
import { phoneRegex, passwordRegex, codeRegex, slideCaptcha, countdown } from '@/utils/utils.js'
import { loginTencentCaptcha, loginUserLogin, loginSendCode, loginCodeLogin } from '@/apis/apis.js'
import useUserStore from '@/stores/useUserStore.js'

const tab = ref(true) // 登录方式
const employeePhoneNumber = ref('18087101569') // 手机号
const loginPassword = ref('123456we') // 密码
const codeval = ref('') // 验证码
const throttle = ref(false) // 节流阀
const countdownText = ref('发送验证码')

const router = useRouter() // 路由器
const userStore = useUserStore() // 用户仓库

const submit = async () => {
  try {
    phoneRegex(employeePhoneNumber.value)
  }
  catch (err) {
    return console.error(err.message)
  }
  if (tab.value) {
    try {
      passwordRegex(loginPassword.value)
      let res = await slideCaptcha()
      throttle.value = true
      await loginTencentCaptcha({ ticket: res.ticket, userIp: '127.0.0.1', randstr: res.randstr })
      res = await loginUserLogin({ employeePhoneNumber: employeePhoneNumber.value, loginPassword: loginPassword.value })
      userStore.updateToken(res.data.userToken)
      userStore.updateUser({ employeeName: res.data.employeeName, employeeRole: res.data.employeeRole })
      router.replace({ name: 'Home' })
    }
    catch (err) {
      console.error(err.message)
    }
  }
  else {
    try {
      codeRegex(codeval.value)
      throttle.value = true
      const res = await loginCodeLogin({ employeePhoneNumber: employeePhoneNumber.value, codeval: codeval.value })
      userStore.updateToken(res.data.userToken)
      userStore.updateUser({ employeeName: res.data.employeeName, employeeRole: res.data.employeeRole })
      router.replace({ name: 'Home' })
    }
    catch (err) {
      console.error(err.message)
    }
  }
  throttle.value = false
} // 登录

const send = async () => {
  try {
    phoneRegex(employeePhoneNumber.value)
    let res = await slideCaptcha()
    await loginTencentCaptcha({ ticket: res.ticket, userIp: '127.0.0.1', randstr: res.randstr })
    countdown(countdownText, 60)
    await loginSendCode({ employeePhoneNumber: employeePhoneNumber.value })
  }
  catch (err) {
    return console.error(err.message)
  }
}

watch(tab, () => {
  employeePhoneNumber.value = ''
  loginPassword.value = ''
  codeval.value = ''
}) // 登录方式切换，表单数据清空
</script>

<template>
  <div class="view">
    <h1>茶千道商家中心</h1>
    <div class="login">
      <div class="tab">
        <div class="tab-item" :class="{ active: tab }" @click="tab = true">账号登录</div>
        <div class="tab-item" :class="{ active: !tab }" @click="tab = false">验证码登录</div>
      </div>
      <div class="tab-content account-login" v-show="tab">
        <div class="phone">
          <span>手机号</span>
          <input type="text" placeholder="输入手机号" v-model.trim="employeePhoneNumber">
        </div>
        <div class="password">
          <span>密码</span>
          <input type="password" placeholder="输入密码" v-model.trim="loginPassword">
        </div>
      </div>
      <div class="tab-content vercode-login" v-show="!tab">
        <div class="phone">
          <span>手机号</span>
          <input type="text" placeholder="输入手机号" v-model.trim="employeePhoneNumber">
        </div>
        <div class="vercode">
          <span>验证码</span>
          <input type="text" placeholder="输入验证码" v-model.trim="codeval">
          <button class="send" @click="send" :disabled="countdownText !== '发送验证码'">{{ countdownText }}</button>
        </div>
      </div>
      <button class="submit" @click="submit" :disabled="throttle">登录</button>
    </div>
  </div>
</template>

<style scoped lang="scss">
.view {
  position: relative;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-image: url(@/assets/login-bg.jpg);
  background-position: center;
  background-repeat: no-repeat;
  background-size: cover;

  &::after {
    content: '';
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    background-color: rgba($color: #000000, $alpha: 0.1);
  }

  h1 {
    z-index: 999;
    margin-bottom: 20px;
    color: var(--el-color-white);
    font-weight: normal;
  }

  .login {
    z-index: 999;
    display: flex;
    flex-direction: column;
    align-items: center;
    padding-top: 20px;
    width: 23%;
    height: 30%;
    background-color: var(--el-color-white);
    border-radius: 10px;

    .tab {
      display: flex;
      justify-content: space-around;
      width: 100%;
      
      .tab-item {
        padding-bottom: 5px;
        cursor: pointer;
        
        &.active {
          border-bottom: 3px solid orange;
        }
      }
    }

    .tab-content {
      padding-bottom: 20px;
      width: 90%;
      height: 60%;

      &.account-login {
        display: flex;
        flex-direction: column;
        
        > div {
          display: flex;
          align-items: center;
          flex: 1;
          border-bottom: 1px solid #eee;

          span {
            width: 70px;
          }

          input {
            border: none;
            outline: none;
            font-size: 16px;
          }
        }
      }

      &.vercode-login {
        display: flex;
        flex-direction: column;
        
        > div {
          display: flex;
          align-items: center;
          flex: 1;
          border-bottom: 1px solid #eee;

          span {
            width: 70px;
          }

          input {
            flex: 1;
            border: none;
            outline: none;
            font-size: 16px;
          }
        }

        .vercode {
          
          .send {
            background-color: transparent;
            border: none;
            color: var(--el-color-primary);
            font-size: 14px;
            cursor: pointer;

            &:hover {
              color: gray;
            }

            &:disabled {
              color: gray;
              cursor: not-allowed;
            }
          }
        }
      }
    }

    .submit {
      width: 90%;
      height: 15%;
      background-color: orange;
      border: none;
      border-radius: 5px;
      cursor: pointer;
    }
  }
}
</style>
